package com.lishuang.manager;

import com.lishuang.DAO.visitLogDAO.visitLog;
import com.lishuang.base.Enums.DeviceStatus;
import com.lishuang.base.ThreadPoolUtil.PriorityDistributedBlockingQueue;
import com.lishuang.base.ThreadPoolUtil.PriorityDistributedThreadPoolExecutor;
import com.lishuang.base.ThreadPoolUtil.ThreadFactory_;
import com.lishuang.domain.json.Device;
import com.lishuang.services.DAOService;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author LiShuang
 * @version 1.0.0
 * @date 2022/11/5 16:15
 */
public class DeviceScheduler {
    private final PriorityDistributedThreadPoolExecutor executor;
    private final int resource=0;//用于回调
    public static int index = 0;

    //用于给前端返回设备状态的Map
    public static Map<String, Device> map = new ConcurrentHashMap();

    public DeviceScheduler() {
        Map<Integer, BlockingQueue<Runnable>> queueMap = new ConcurrentHashMap();
        for(int i = 1;i<10;++i){
            queueMap.put(i, new PriorityBlockingQueue<>());
        }
        PriorityDistributedBlockingQueue queue = new PriorityDistributedBlockingQueue(queueMap);
        executor = new PriorityDistributedThreadPoolExecutor(
            50,50,Integer.MAX_VALUE, TimeUnit.DAYS,
                queue,
                new ThreadFactory_()
        );
    }

    public void startDeviceThread(Device device) throws Exception {
        executor.execute(device);
    }
    public void stopDeviceThread(Device device) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InterruptedException {
        //使用反射机制来停止它
        Class clazz = device.getClass();
        Method[] methods = clazz.getDeclaredMethods();
        //注释的部分是为了确定方法的可用性
//        for (int i = 0; i < methods.length; i++) {
//            System.out.println("----------第" + (i + 1) + "个方法----------");
//            Method method = methods[i];
//            // 获取方法的返回值类类型
//            Class<?> returnType = method.getReturnType();
//            System.out.println("获取方法的返回值类类型:" + returnType.getName());
//            // 获取方法的名称
//            System.out.println("获取方法的名称：" + method.getName());
//            // 获取参数
//            Class<?>[] parameterTypes = method.getParameterTypes();
//            if (parameterTypes.length > 0) {
//                System.out.println("获取所有参数如下：");
//                for (int i1 = 0; i1 < parameterTypes.length; i1++) {
//                    System.out.println("   第" + (i1 + 1) + "个参数：" + parameterTypes[i1]);
//                }
//            }
//        }
        Method getName1 = clazz.getDeclaredMethod("setInterrupted");
        getName1.invoke(device);
    }

    public void stopDeviceThread(String uuid){
        try {
            Device device = map.get(uuid);
            if(device.getDeviceStatus()!= DeviceStatus.running)
                return;
            stopDeviceThread(device);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void deleteDeviceThread(String uuid){
        stopDeviceThread(uuid);
        map.remove(uuid);
    }
    public Map<String, Device> getMap() {
        return map;
    }


    public List<visitLog> demo() throws Exception {
        for (String key:map.keySet()) {
            DAOService.submit(map.get(key));
        }
        return DAOService.start();
    }

    @Deprecated
    public void setMap(Map<String, Device> map) {
        DeviceScheduler.map = map;
    }
//    public static Map<Integer,String> getConnectedDevice(){
//        Map<Integer, String> retmap = new ConcurrentHashMap();
//        for (String key: map.keySet()){
//            retmap.put(key,map.get(key).getDeviceName());
//        }
//        return retmap;
//    }
}
